package com.share.sharedevice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.sharedevice.entity.Cabinet;
import com.share.sharedevice.entity.CabinetSlot;
import com.share.sharedevice.entity.CabinetType;
import com.share.sharedevice.mapper.CabinetMapper;
import com.share.sharedevice.service.ICabinetService;
import com.share.sharedevice.service.ICabinetSlotService;
import com.share.sharedevice.service.ICabinetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CabinetServiceImpl extends ServiceImpl<CabinetMapper, Cabinet> implements ICabinetService {


    @Autowired
    private CabinetMapper cabinetMapper;

    @Autowired
    private ICabinetTypeService cabinetTypeService;

    @Autowired
    private ICabinetSlotService cabinetSlotService;

    /**
     * 查询柜机列表
     * @param cabinet
     * @return
     */
    @Override
    public List<Cabinet> selectCabinetList(Cabinet cabinet) {
        List<Cabinet> cabinetList = cabinetMapper.selectCabinetList( cabinet);
        return cabinetList;
    }

    /**
     * 根据id查询柜机
     * @param id
     * @return
     */
    @Override
    public Cabinet getCabinetById(Long id) {

        Cabinet cabinet = cabinetMapper.getCabinetById(id);

        return cabinet;
    }

    /**
     * 删除
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Long> ids) {
        cabinetMapper.deleteByIds(ids);
    }

    /**
     * 新增
     * @param cabinet
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Cabinet cabinet) {

        cabinet.setDelFlag("0");

        cabinet.setLocationId(1L);

        //查询所需要的柜机类型是否存在
        Long cabinetTypeId = cabinet.getCabinetTypeId();

        Long cabinetCabinetTypeId = cabinet.getCabinetTypeId();

        if (cabinetCabinetTypeId == null){
            throw new RuntimeException("请选择柜机类型");
        }

        CabinetType cabinetType = cabinetTypeService.selectCabinetTypeById(cabinetTypeId);

        if (cabinetType == null) {
            throw new RuntimeException("不存在的柜机类型");
        }

        cabinet.setTotalSlots(cabinetType.getTotalSlots());

        cabinet.setFreeSlots(cabinetType.getTotalSlots());

        cabinet.setStatus("0");

        cabinet.setUsedSlots(0);

        cabinet.setAvailableNum(cabinetType.getTotalSlots());

        cabinet.setCreateTime(new Date());

        cabinet.setUpdateTime(new Date());

        String name = cabinet.getName();

        if (name != null && name.length() > 0) {
            Cabinet cabinetByName = cabinetMapper.selectCabinetByName(name);
            if (cabinetByName != null) {
                throw new RuntimeException("名称已存在");
            }
        }else if (name == null || name.length() == 0){
            throw new RuntimeException("名称不能为空");
        }

        String cabinetNo = cabinet.getCabinetNo();
        if (cabinetNo != null && cabinetNo.length() > 0) {
            Cabinet cabinetByCabinetNo = cabinetMapper.selectCabinetByCabinetNo(cabinetNo);
            if (cabinetByCabinetNo != null) {
                throw new RuntimeException("编号已存在");
            }
        }else if (cabinetNo == null || cabinetNo.length() == 0){
            throw new RuntimeException("编号不能为空");
        }

        cabinetMapper.add(cabinet);

        CabinetSlot cabinetSlot = new CabinetSlot();
        cabinetSlotService.add(cabinet,cabinetSlot);
    }


    /**
     * 修改
     * @param cabinet
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCabinet(Cabinet cabinet) {

        Long id = cabinet.getId();

        Cabinet cabinetById = cabinetMapper.getCabinetById(id);

        Long cabinetTypeId = cabinetById.getCabinetTypeId();

        Long cabinetCabinetTypeId = cabinet.getCabinetTypeId();

        CabinetType cabinetType = cabinetTypeService.selectCabinetTypeById(cabinetCabinetTypeId);

        cabinet.setTotalSlots(cabinetType.getTotalSlots());


        if (id == null){
            throw new RuntimeException("请选择要修改的柜机");
        }

        cabinet.setUpdateTime(new Date());

        String cabinetNo = cabinet.getCabinetNo();

        if (cabinetNo == null || cabinetNo.length() == 0){
            throw new RuntimeException("编号不能为空");
        }

        String name = cabinet.getName();

        if (name == null || name.length() == 0){
            throw new RuntimeException("名称不能为空");
        }

        cabinetMapper.updateCabinet(cabinet);

        //如果柜机类型改变，则删除所有插槽信息，重新添加

        cabinetSlotService.deleteALL(id);
        CabinetSlot cabinetSlot = new CabinetSlot();
        cabinetSlotService.add(cabinet,cabinetSlot);

    }

    /**
     * 查询未使用的柜机
     * @param keyword
     * @return
     */
    @Override
    public List<Cabinet> searchNoUseList(String keyword) {
        List<Cabinet> cabinetList = cabinetMapper.searchNoUseList(keyword);
        return cabinetList;
    }


}
